/*
 Copyright (c) Whitemice Consulting, 2007, 2008, 2009
 Adam Tauno Williams <awilliam@whitemice.org>
 License: MIT/X11

 Permission is hereby granted, free of charge, to any person obtaining a copy
 of this software and associated documentation files (the "Software"), to deal
 in the Software without restriction, including without limitation the rights
 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 copies of the Software, and to permit persons to whom the Software is
 furnished to do so, subject to the following conditions:

 The above copyright notice and this permission notice shall be included in
 all copies or substantial portions of the Software.
 
 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 THE SOFTWARE.
*/
using System;
using System.Collections;
using System.Collections.Generic;
using Gtk;
using Whitemice.ZOGI;

namespace Whitemice.ZOGI.GtkWidgets
{
  [System.ComponentModel.ToolboxItem(true)]
	public class ProjectNodeView : CompactNodeView
	{
		public 		EntityEventHandler	OnProjectEdit;
		public 		EntityEventHandler	OnProjectUnassign;
	
		public ProjectNodeView() 
			: base(null, false, new Gtk.NodeStore(typeof(ProjectListNode)))
		{
			AppendTextColumn("Id", 0, true, false, true);
			AppendTextColumn("Name", 1, true, true, true);
			AppendTextColumn("Kind", 2, true, false, true);
			AppendTextColumn("Owner", 3, true, true, true);
			AppendTextColumn("Number", 4, true, true, true);
			NodeStore.Clear();
			ButtonPressEvent += new ButtonPressEventHandler(OnButonPressEvent);
			RowActivated += new RowActivatedHandler(OnRowActivated);
		} // end ctor
		
		public override void Fill(IEnumerable<Entity> data, string message)
		{
			this.NodeStore.Clear();
			foreach(Entity entity in data)
			{
			  if (entity is Project)
				  this.NodeStore.AddNode(new ProjectListNode(entity as Project));
		  }
			if ((message != null) && (statusLabel != null))
				statusLabel.Text = message;		
		} // end Fill
		
		protected void ProjectEdit(object _sender, EventArgs _args)
		{ 
			OnProjectEdit(
				(_sender as PopUpMenuItem).ContextObject,
				"edit",
				null); 
		} // end ProjectEdit
		
		protected void ProjectUnassign(object _sender, EventArgs _args)
		{ 
			OnProjectUnassign(
				(_sender as PopUpMenuItem).ContextObject,
				"unassign",
				null); 
		} // end ProjectUnassign
		
		[GLib.ConnectBefore]
		protected void OnRowActivated(object sender, Gtk.RowActivatedArgs args)
		{
		 	ITreeNode				node;

			node = (sender as NodeView).NodeSelection.SelectedNode;
			if ((node is ProjectListNode) && (this.OnProjectEdit != null))
	  	  OnProjectEdit(((ProjectListNode)node).Project, "edit", null);
		}				
		
		[GLib.ConnectBefore]		
		public void OnButonPressEvent(object sender, ButtonPressEventArgs args)
		{
			ITreeNode				node;
			Project					project;

			if (PopupEnabled)
			{
        node = ((NodeView)sender).NodeSelection.SelectedNode;
			  if (node is ProjectListNode)
				  project = ((ProjectListNode)node).Project;
				  else return;
  			if (args.Event.Button == 3) 
	  		{
		  		PopupMenuBuilder  menu;
  
	    		menu = new PopupMenuBuilder();
	    	  menu.Append("Edit", project, new EventHandler(ProjectEdit), 
	    	              (OnProjectEdit != null));
	  	    menu.AppendSeperator();
 				  menu.Append("Unassign", project, new EventHandler(ProjectUnassign), 
 				              ((OnProjectUnassign != null) && (project.CanModify)));
   				menu.Run();
	  		}
	    }
		} // end OnButonPressEvent 
		
		public override void Remove(Entity _entity)
		{
		  /* TODO: Remove in ProjectNodeView */
		}		
	} // end ProjectNodeView 
} // end namespace 